<?php
/**
 * 会员模块
 * 
 * @author river_he@eueang.com
 */
final class Kit_Model_Shoper extends Kit_Model_Abstract{
	/**
	 * 获取单个会员信息
	 * 
	 * @param array|string $where
	 * @return array
	 */
	public function getItem($where){
		return $this->getShoperDao()->getItem($where);
	}
	
	/**
	 * 获取会员列表
	 * 
	 * @param array|string $where
	 * @param string $order
	 * @param int $page
	 * @param int $psize
	 * @return array
	 */
	public function getItems($where, $sort='', $page=0, $psize=10){
		return $this->getShoperDao()->getItems($where, $sort, $page, $psize);
	}
	
	/**
	 * 获取店主道具列表
	 * 
	 * @param int $shoperId
	 * @param int $status
	 * @param int $page
	 * @param int $psize
	 * @retrun array
	 */
	public function getProps($shoperId, $status, $page=0, $psize=10){
		$return = array();
		if($shoperId){
			$where = array('nUid='.$shoperId);
			if(0 < $status){
				array_push($where, 'nStatus='.$status);
			}elseif(0 == $status){
				array_push($where, 'nStatus IN(1,2)');
			}
			$data = $this->getShoperPropDao()->getItems($where, '', $page, $psize);
			$pager = array();
			if($data){
				$total = $this->getShoperPropDao()->getWhereTotal($where);
				$pager['page'] = $page;
				$pager['psize'] = $psize;
				$pager['ptotal'] = ceil($total/$psize);
				$pager['rtotal'] = $total;
			}
			$return['data'] = $data;
			$return['pager'] = $pager;
		}
		return $return;
	}
	
	/**
	 * 使用道具
	 * 
	 * @param int $shoperId
	 * @param int $shopId
	 * @param int $shopPropId
	 * @return array|int -1-2:no prop -3-4:no shop -3:not shop owner
	 */
	public function useProp($shoperId, $shopId, $shopPropId){
		$shoperProp = $this->getShoperPropDao()->getItem('id='.$shopPropId);
		if(!$shoperProp){
			return -1;
		}elseif(1 > $shoperProp['nTotal']){
			return -2;
		}elseif($shoperProp['nUid'] != $shoperId){
			return -3;
		}
		$prop = $this->getPropertyDao()->getItem('id='.$shoperProp['nPid']);
		if(!$prop){
			return -4;
		}
		$shop = $this->getShopDao()->getItem('id='.$shopId);
		if(!$shop){
			return -5;
		}elseif($shop['nUid'] != $shoperId){
			return -6;
		}
		$whas = array('nShopId='.$shop['id']);
		array_push($whas, 'nPid='.$prop['id']);
		$hasUse = $this->getShopPropDao()->getItem($whas);
		$expire = Kit_Util::getTimeStamp();
		$swhere = array('id='.$shoperProp['id']);
		array_push($swhere, 'nTotal='.$shoperProp['nTotal']);
		if($this->getShoperPropDao()->update($swhere, array('nTotal'=>$shoperProp['nTotal']-1))){
			if($hasUse){
				$expire = $expire>$hasUse['nExpTime']?$expire:$hasUse['nExpTime'];
				$expire += $prop['nValid'];
				$set = array(
				'nStatus'=>2,
				'nExpTime'=>$expire
				);
				$this->getShopPropDao()->update('id='.$hasUse['id'], $set);
				$hasUse['nStatus'] = 2;
				$hasUse['nExpTime'] = $expire;
				return $hasUse;
			}else{
				$expire += $prop['nValid'];
				$record = array(
				'nUid'=>$shoperId,
				'nShopId'=>$shop['id'],
				'nPid'=>$prop['id'],
				'sName'=>$prop['sName'],
				'nRate'=>$prop['nRate'],
				'nFun'=>$prop['nFun'],
				'sUrl'=>$prop['sUrl'],
				'nStatus'=>2,
				'nUseTime'=>Kit_Util::getTimeStamp(),
				'nExpTime'=>$expire,
				'nCrTime'=>Kit_Util::getTimeStamp()
				);
				$id = $this->getShopPropDao()->add($record);
				$record['id'] = $id;
				return $record;
			}
		}else{
			return -7;
		}
		return -8;
	}
	
	/**
	 * 使用装修材料
	 * 
	 * @param $shoperId
	 * @param $shopId
	 * @param $shoperDecoId
	 * @param $x
	 * @param $y
	 * @return array|int -1:no deco -2:nodeco -3
	 */
	public function useDeco($shoperId, $shopId, $shoperDecoId, $x, $y){
		//@todo 位置还有待确认
		$shoperDeco = $this->getShoperDecoDao()->getItem('id='.$shoperDecoId);
		if(!$shoperDeco){
			return -1;
		}elseif(1 > $shoperDeco['nTotal']){
			return -2;
		}elseif($shoperDeco['nUid'] != $shoperId){
			return -3;
		}
		$decorate = $this->getDecorateDao()->getItem('id='.$shoperDeco['nDid']);
		if(!$decorate){
			return -4;
		}
		$shop = $this->getShopDao()->getItem('id='.$shopId);
		if(!$shop){
			return -5;
		}elseif($shop['nUid'] != $shoperId){
			return -6;
		}
		$dwhere = array('id='.$shoperDeco['id']);
		array_push($dwhere, 'nTotal='.$shoperDeco['nTotal']);
		if($this->getShoperDecoDao()->update($dwhere, array('nTotal'=>$shoperDeco['nTotal']-1))){
			$expire = Kit_Util::getTimeStamp();
			if($decorate['nValid'] == 0){
				$expire = 0;
			}else{
				$expire += $decorate['nValid'];
			}
			$record = array(
			'nUid'=>$shop['nUid'],
			'nShopId'=>$shop['id'],
			'nDid'=>$decorate['id'],
			'sName'=>$decorate['sName'],
			'nPx'=>$x,
			'nPy'=>$y,
			'nPz'=>0,
			'nExpTime'=>$expire,
			'nPosit'=>$decorate['nPosit'],
			'sUrl'=>$decorate['sUrl'],
			'nStatus'=>2,
			'nUseTime'=>Kit_Util::getTimeStamp(),
			'nCrTime'=>Kit_Util::getTimeStamp()
			);
			$id = $this->getShopDecoDao()->add($record);
			$record['id'] = $id;
			return $record;
		}else{
			return -7;
		}
		return -8;
	}
	
	/**
	 * 获取店主装修列表
	 * 
	 * @param int $shoperId
	 * @param int $status
	 * @param int $page
	 * @param int $psize
	 * @return array
	 */
	public function getDecos($shoperId, $status, $page=0, $psize=10){
		$return = array();
		if($shoperId){
			$where = array('nUid='.$shoperId);
			if(0 < $status){
				array_push($where, 'nStatus='.$status);
			}elseif(0 == $status){
				array_push($where, 'nStatus IN(1,2)');
			}
			$data = $this->getShoperDecoDao()->getItems($where, '', $page, $psize);
			$pager = array();
			if($data){
				$total = $this->getShoperDecoDao()->getWhereTotal($where);
				$pager['page'] = $page;
				$pager['psize'] = $psize;
				$pager['ptotal'] = ceil($total/$psize);
				$pager['rtotal'] = $total;
			}
			$return['data'] = $data;
			$return['pager'] = $pager;
		}
		return $return;
	}
	
	/**
	 * 获取好友列表
	 * 
	 * @param int $shoperId
	 * @param int $sort 0所有店铺等级排序 1蛋糕店排序2冰淇淋排序3汉堡排序
	 * @param int $page
	 * @param int $psize
	 * @return array
	 */
	public function getFriends($shoperId, $sort=1, $page=1, $psize=10){
		$return = array();
		if($shoperId){
			$rquery = 'SELECT';
			$tquery = 'SELECT COUNT(a.id)';
			$page = $page>0?$page:1;
			$psize = $psize>0?$psize:10;
			$offset = ($page-1)*$psize;
			if($sort == 0){
				$rquery .= ' a.*, b.nShops, b.nShopLevel';
				$query = ' FROM '.$this->getShoperFriendDao()->getTableName().' as a, '.$this->getShoperDao()->getTableName().' as b';
				$query .= ' WHERE a.nUid='.$shoperId;
				$query .= ' AND b.id=a.nFid';
				$tquery .= $query;
				$rquery .= $query;
				$rquery .= ' ORDER BY b.nShopLevel DESC';
			}else{
				$rquery .= ' a.*, d.nShops, c.nLevel as nShopLevel';
				$query = ' FROM '.$this->getShoperFriendDao()->getTableName().' as a, '.$this->getShopDao()->getTableName().' as b, '.$this->getShopStatDao()->getTableName().' as c, '.$this->getShoperDao()->getTableName().' as d';
				$query .= ' WHERE a.nUid='.$shoperId;
				$query .= ' AND b.nUid=a.nFid';
				$query .= ' AND d.id=b.nUid';
				$query .= ' AND b.nType='.$sort;
				$query .= ' AND c.nShopId=b.id';
				$tquery .= $query;
				$rquery .= $query;
				$rquery .= ' ORDER BY c.nLevel DESC';
			}
			$rquery .= ' LIMIT '.$offset.','.$psize;
			$data = $this->getShopDao()->getAdapter()->queryAll($rquery);
			if($data){
				$total = $this->getShopDao()->getAdapter()->queryTotal($tquery);
				foreach ($data as &$row){
					if($row['nShops']>0){
						$row['aShops'] = $this->getShopDao()->getItems('nUid='.$row['id'], 'id ASC', 1, 5);
					}else{
						$row['aShops'] = array();
					}
				}
				$return['data'] = $data;
				$return['pager'] = array(
				'page'=>$page,
				'psize'=>$psize,
				'ptotal'=>ceil($total/$psize),
				'rtotal'=>$total
				);
			}
		}
		return $return;
	}
}
//end